package com.envocabulary.widget_list;

import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.RemoteViews;

import com.envocabulary.Ultis.Reference;

public class ListWidgetProvider extends AppWidgetProvider {
	private static final String tag = "TestListWidgetProvider";

	public static final String ACTION_LIST_CLICK = "com.example.widget_list.listclick";
	public static final String ACTION_REFRESH_BUTTON_CLICK = "com.example.widget_list.refreshbuttonclick";
	public static final String EXTRA_LIST_ITEM_TEXT = "com.example.widget_list.list_item_text";
	public static final String EXTRA_LIST_ITEM_ID = "com.example.widget_list.list_item_id";
	public static final String EXTRA_LIST_ITEM_PHONETIC = "com.example.widget_list.list_item_phonetic";
	public static final String EXTRA_LIST_ITEM_MEANING = "com.example.widget_list.list_item_meaning";
	public static final String EXTRA_LIST_ITEM_PAST_SIMPLE = "com.example.widget_list.list_item_past_simple";

	private PendingIntent service = null;

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		// Log.d(tag, "onUpdate called");

		final int N = appWidgetIds.length;
		// Log.d(tag, "Number of widgets: " + N);

		for (int i = 0; i < N; i++) {
			int appWidgetId = appWidgetIds[i];
			updateAppWidget(context, appWidgetManager, appWidgetId);
		}

		super.onUpdate(context, appWidgetManager, appWidgetIds);
	}

	private void updateAppWidget(Context context,
			AppWidgetManager appWidgetManager, int appWidgetId) {
		Log.d(tag, "onupdate called for widget:" + appWidgetId);

		// //////////////////////////////////////
		final AlarmManager m = (AlarmManager) context
				.getSystemService(Context.ALARM_SERVICE);

		final Calendar TIME = Calendar.getInstance();
		TIME.set(Calendar.HOUR_OF_DAY, 0);
		TIME.set(Calendar.MINUTE, 0);
		TIME.set(Calendar.SECOND, 0);

		final Intent in = new Intent(context, AutoRefreshService.class);
		in.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);

		if (service == null) {
			service = PendingIntent.getService(context, 0, in,
					PendingIntent.FLAG_CANCEL_CURRENT);
		}
		if(Reference.TYPE_TIME == 1)
			m.setRepeating(AlarmManager.RTC, TIME.getTime().getTime(),
					AlarmManager.INTERVAL_DAY, service);
		else
			m.setRepeating(AlarmManager.RTC, TIME.getTime().getTime(),
					1440000* Reference.TYPE_TIME, service);
		// /////////////////////////////

		final RemoteViews rv = new RemoteViews(context.getPackageName(),
				R.layout.widget_layout);

		rv.setEmptyView(R.id.listwidget_list_view_id,
				R.id.listwidget_empty_view_id);

		// chi dinh service de cung cap data cho widget
		final Intent intent = new Intent(context, MyRemoteViewsService.class);
		intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);

		/*
		 * intent.setData( Uri.parse( intent.toUri(Intent.URI_INTENT_SCHEME)));
		 */
		rv.setRemoteAdapter(appWidgetId, R.id.listwidget_list_view_id, intent);

		// cai dat mot list view callback.
		// can mot pending intent duy nhat theo widget id. Gui mot message se
		// catch trong OnRecive
		Intent onListClickIntent = new Intent(context, ListWidgetProvider.class);
		onListClickIntent.setAction(ListWidgetProvider.ACTION_LIST_CLICK);
		onListClickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
				appWidgetId);

		onListClickIntent.setData(Uri.parse(onListClickIntent
				.toUri(Intent.URI_INTENT_SCHEME)));

		final PendingIntent onListClickPendingIntent = PendingIntent
				.getBroadcast(context, 0, onListClickIntent,
						PendingIntent.FLAG_UPDATE_CURRENT);

		rv.setPendingIntentTemplate(R.id.listwidget_list_view_id,
				onListClickPendingIntent);

		// set button setting click event
		Intent onSettingIntent = new Intent(context, SettingsActivity.class);
		onSettingIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
				appWidgetId);
		final PendingIntent onSettingPendingIntent = PendingIntent.getActivity(
				context, 1, onSettingIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

		rv.setOnClickPendingIntent(R.id.btnSetting, onSettingPendingIntent);

		// set button refresh click event
		Intent onRefreshClickIntent = new Intent(context,
				ListWidgetProvider.class);
		onRefreshClickIntent
				.setAction(ListWidgetProvider.ACTION_REFRESH_BUTTON_CLICK);
		onRefreshClickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
				appWidgetId);

		final PendingIntent onRefreshClickPendingIntent = PendingIntent
				.getBroadcast(context, 0, onRefreshClickIntent,
						PendingIntent.FLAG_UPDATE_CURRENT);

		rv.setOnClickPendingIntent(R.id.btnRefresh, onRefreshClickPendingIntent);

		// update the widget
		appWidgetManager.updateAppWidget(appWidgetId, rv);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		if (intent.getAction().equals(ListWidgetProvider.ACTION_LIST_CLICK)) {
			dealwithListAction(context, intent);
			return;
		} else if (intent.getAction().equals(
				ListWidgetProvider.ACTION_REFRESH_BUTTON_CLICK)) {
			// Toast t = Toast.makeText(context, "Refresh clicked!",
			// Toast.LENGTH_SHORT);
			// t.show();
			int appWidgetId = intent.getIntExtra(
					AppWidgetManager.EXTRA_APPWIDGET_ID,
					AppWidgetManager.INVALID_APPWIDGET_ID);
			AppWidgetManager.getInstance(context)
					.notifyAppWidgetViewDataChanged(appWidgetId,
							R.id.listwidget_list_view_id);
			return;
		}

		super.onReceive(context, intent);
	}

	private void dealwithListAction(Context context, Intent intent) {
		String clickedItemText = intent
				.getStringExtra(ListWidgetProvider.EXTRA_LIST_ITEM_TEXT);

		String clickedItemId = intent
				.getStringExtra(ListWidgetProvider.EXTRA_LIST_ITEM_ID);
		String clickedItemPhonetic = intent
				.getStringExtra(ListWidgetProvider.EXTRA_LIST_ITEM_PHONETIC);
		String clickedItemMeaning = intent
				.getStringExtra(ListWidgetProvider.EXTRA_LIST_ITEM_MEANING);
		
		String clickedItemPast = intent
				.getStringExtra(ListWidgetProvider.EXTRA_LIST_ITEM_PAST_SIMPLE);

		// Toast t = Toast.makeText(context, clickedItemText,
		// Toast.LENGTH_SHORT);
		// t.show();

		Intent i = new Intent(context, DetailActivity.class);
		i.putExtra(ListWidgetProvider.EXTRA_LIST_ITEM_TEXT, clickedItemText);
		i.putExtra(ListWidgetProvider.EXTRA_LIST_ITEM_ID, clickedItemId);
		i.putExtra(ListWidgetProvider.EXTRA_LIST_ITEM_PHONETIC,
				clickedItemPhonetic);
		i.putExtra(ListWidgetProvider.EXTRA_LIST_ITEM_MEANING,
				clickedItemMeaning);
		i.putExtra(ListWidgetProvider.EXTRA_LIST_ITEM_PAST_SIMPLE,
				clickedItemPast);
		
		i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(i);
	}

	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		Log.d(tag, "onDelete called");
		super.onDeleted(context, appWidgetIds);
	}

	@Override
	public void onEnabled(Context context) {
		Log.d(tag, "onEnabled called");
		super.onEnabled(context);
	}

	@Override
	public void onDisabled(Context context) {
		Log.d(tag, "onDisabled called");
		final AlarmManager m = (AlarmManager) context
				.getSystemService(Context.ALARM_SERVICE);

		m.cancel(service);
		super.onEnabled(context);
	}

}
